package v7

import (
	"database/sql"
	"go-demo/orm/model"
)

// DB 是 sql.DB 的一个封装。
type DB struct {
	registry model.Registry
	db       *sql.DB
}

//
//func (d *DB) QueryContext(ctx context.Context, sql string, args ...any) (*DB, error) {
//	d.db.QueryContext()
//}

type DBOption func(*DB)

func Open(driver string, dsn string, opts ...DBOption) (*DB, error) {
	db, err := sql.Open(driver, dsn)
	if err != nil {
		return nil, err
	}
	return OpenDB(db, opts...)
}

func OpenDB(db *sql.DB, opts ...DBOption) (*DB, error) {
	res := &DB{
		registry: model.NewRegistry(),
		db:       db,
	}
	for _, opt := range opts {
		opt(res)
	}
	return res, nil
}
